<template>
  <div class="treeMain">
    <a-tree
      checkable
      showLine
      checkStrictly
      :defaultExpandAll='true'
      v-model="checkedKeys"
      @select="onSelect"
      @check='onCheck'
      :selectedKeys="selectedKeys"
      :treeData="treeData"
      v-if="treeData && treeData.length > 0"
    />
  </div>
</template>
<script>
import copyACtion from '@/views/singleProject/fileSingle/el-common-js/copy'
import { treeList,treeData } from './dataBase'
export default {
  mixins: [copyACtion],
  data () {
    return {
      defaultCheckedKeys: [],
      expandedKeys: [],
      checkedKeys: [],
      selectedKeys: [],
      treeData: [],
    //  lastCheckedKeys: JSON.parse(sessionStorage.getItem('lastTreeCheckedKeys'))
    }
  },
  created () {
    this.treeData = treeList;
    this.initTreeData(this.treeData)
    this.checkedKeys.push(this.treeData[0].oid)
    sessionStorage.setItem('lastTreeCheckedKeys',JSON.stringify(this.treeData[0]))   //每次刷新页面都会存整个树结构，后面点击再另存
    sessionStorage.setItem('treeDataMax',JSON.stringify(this.treeData[0]))
    let session = {}
    if(sessionStorage.getItem('fileSingle') && JSON.parse(sessionStorage.getItem('fileSingle')).tableArr.length>0){
      session = { treeItem: this.treeData[0], layerItem: false , tableArr: JSON.parse(sessionStorage.getItem('fileSingle')).tableArr, }
    }else{
      session = { treeItem: this.treeData[0], layerItem: false , tableArr: [], }
    }
    this.setFileSingle(session)
    this.$emit('noticeInitData',treeData)
    return false;
    let _this = this
    this.getTreeData({ callback: (res) => {
     console.log(res, '35')
      if (res.code === 200) {
        _this.treeData = res.data
        _this.initTreeData(_this.treeData)
        _this.checkedKeys.push(_this.treeData[0].oid)
        sessionStorage.setItem('lastTreeCheckedKeys',JSON.stringify(_this.treeData[0]))   //每次刷新页面都会存整个树结构，后面点击再另存
        sessionStorage.setItem('treeDataMax',JSON.stringify(_this.treeData[0]))
        let session = {}
        if(sessionStorage.getItem('fileSingle') && JSON.parse(sessionStorage.getItem('fileSingle')).tableArr.length>0){
          session = { treeItem: _this.treeData[0], layerItem: false , tableArr: JSON.parse(sessionStorage.getItem('fileSingle')).tableArr, }
        }else{
          session = { treeItem: _this.treeData[0], layerItem: false , tableArr: [], }
        }
        _this.setFileSingle(session)
        _this.$emit('noticeInitData', res.data[0])
        //  if(lastCheckedKeys && lastCheckedKeys !==''){
        //    _this.onCheck(lastCheckedKeys);
        // }else{
        //    _this.checkedKeys.push(_this.treeData[0].oid)
        // }
      }
    }})
  },
  methods: {
    onCheck (checkedKeys) {
      if(checkedKeys.checked.length > 1){
        this.checkedKeys = [checkedKeys.checked[checkedKeys.checked.length - 1]]
      }else{
         this.checkedKeys = [this.treeData[0].oid]
      }
      this.cancal(this.checkedKeys)
    },
    onSelect (selectedKeys) {
      this.checkedKeys = selectedKeys
      this.cancal(this.checkedKeys)
    },
    cancal (arr) {
      let _this = this
      let fileSingleData = JSON.parse(sessionStorage.getItem('fileSingle'))
      if (fileSingleData.treeItem.oid == arr[0]){
          sessionStorage.setItem('lastTreeCheckedKeys',JSON.stringify(fileSingleData.treeItem));
          _this.$emit('noticeInitData', fileSingleData.treeItem)
          return
        }
      this.initTreeData(this.treeData, {oid: arr[0], callback: (res) => {
        if (res.code === 200) {
          // let session = { treeItem: res.data, layerItem: res.data.layerItem ? true : false , tableArr: [], }
          sessionStorage.setItem('lastTreeCheckedKeys',JSON.stringify(res.data));
          let session = {}
          if(sessionStorage.getItem('fileSingle') && JSON.parse(sessionStorage.getItem('fileSingle')).tableArr.length>0){
            session = { treeItem: res.data, layerItem: res.data.layerItem ? true : false, tableArr: JSON.parse(sessionStorage.getItem('fileSingle')).tableArr, }
          }else{
            session = { treeItem: res.data, layerItem: res.data.layerItem ? true : false , tableArr: [], }
          }
          _this.setFileSingle(session)
          _this.$emit('noticeInitData', res.data)
        }
      }})
    },
    /**
     * 初始化treeData数据
     */
    initTreeData (data, callbackObj) {
      if (callbackObj) {
        var { oid, callback } = callbackObj
      }
      data.forEach(el => {
        el.title = el.name
        el.key = el.oid
        el.children = el.childs
        if (callbackObj) {
          if (oid == el.oid) {
            return callback({code: 200, data: el})
          }
        }
        if (el.childs && el.childs.length !== 0) {
          el.layerItem = false
          this.initTreeData(el.childs, callbackObj)
        } else {
          el.layerItem = true
          return
        }
      })
    }
  },
}
</script>
<style lang="less" scoped>
.treeMain {
  width: 300px;
  padding: 10px 0 0 20px;
  box-sizing: border-box;
  /deep/.ant-tree {
    li {
      padding: 0;
      line-height: 30px;
      .ant-tree-switcher {
        margin-top: 4px;
      }
      .ant-tree-node-content-wrapper{
        padding: 4px 10px;
        height: auto !important;
        border-radius: 4px;
        font-size: 14px;
        color: #333333;
        &:hover{
          background: #E8F0FD;
        }
      }
    }
  }
}
/deep/ .ant-tree li .ant-tree-node-content-wrapper{
    max-width: 235px !important;
        overflow: hidden !important;
        white-space: nowrap !important;
        text-overflow: ellipsis !important;
 }
</style>